package com.vodafone.mapreduce.reduce;

import java.util.List;
import java.util.Map;

/**
 * TODO: Insert your comments here!
 */
public class WordFrequencyReduceFunction extends ReduceFunction<String, Integer> {

    public WordFrequencyReduceFunction(List<Map<String, List<Integer>>> combineResults) {
        super(combineResults);
    }

    @Override
    protected void doReduce(String l, List<Integer> newWs, List<Integer> oldWs) {
        oldWs.addAll(newWs);
        doCombine(l, oldWs);
    }

    protected void doCombine(String l, List<Integer> ws) {
        Integer combined = 0;
        for (Integer w : ws) {
            if (w != null) {
                combined += w;
            }
        }
        ws.clear();
        ws.add(combined);
    }

}
